package cn.b.sky.modeler.controller.api;

import cn.b.sky.modeler.domain.Group;
import cn.b.sky.modeler.domain.User;
import cn.b.sky.modeler.model.common.GroupRepresentation;
import cn.b.sky.modeler.model.common.UserRepresentation;
import cn.b.sky.modeler.service.exception.NotFoundException;
import cn.b.sky.modeler.util.SecurityUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: yueding
 * @Description:
 * @Date: Created in 11:21 2017/11/21
 * @Modified By:
 * Copyright(c) cai-inc.com
 */
@RestController
@RequestMapping("/app/*")
public class AccountResource {

    /**
     * GET /rest/account -> get the current user.
     */
    @RequestMapping(value = "/rest/account", method = RequestMethod.GET, produces = "application/json")
    public UserRepresentation getAccount() {
        User userInformation = SecurityUtils.getCurrentUserObject();
        if (userInformation != null) {
            UserRepresentation userRepresentation = new UserRepresentation(userInformation);
            if (userInformation.getGroups() != null) {
                for (Group group : userInformation.getGroups()) {
                    userRepresentation.getGroups().add(new GroupRepresentation(group));
                }
            }
            if (userInformation.getPrivileges() != null) {
                for (String privilege : userInformation.getPrivileges()) {
                    userRepresentation.getPrivileges().add(privilege);
                }
            }
            return userRepresentation;
        } else {
            throw new NotFoundException();
        }
    }
}
